<template>
    <div style="min-width: 829px">
        <div v-if="$route.path == '/requisition'">
            <div class="vs-search-box">
                <el-form v-search-box-enter ref="form" :inline="true" :model="paging" label-width="110px">
                    <el-form-item label="商品系列：">
                        <el-input style="width: 200px" placeholder="请输入商品系列" v-model="paging.goods_name" clearable>
                            <i slot="prefix" class="el-input__icon el-icon-search"></i>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="订单状态：">
                        <el-select style="width: 200px" clearable v-model="paging.status" placeholder="请选择">
                            <el-option label="待审核" value="1"> </el-option>
                            <el-option label="已审核" value="2"> </el-option>
                            <el-option label="已驳回" value="3"> </el-option>
                            <el-option label="已取消" value="4"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="申请人：">
                        <el-input style="width: 200px" placeholder="请输入申请人" v-model="paging.create_user" clearable>
                            <i slot="prefix" class="el-input__icon el-icon-search"></i>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="选择时间：">
                        <el-date-picker v-model="paging.date" type="daterange" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd" start-placeholder="开始时间" end-placeholder="结束时间"
                            style="width: 260px"> </el-date-picker>
                    </el-form-item>
                    <el-form-item class="vs-right-btns">
                        <el-button @click="reset">重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="vs-table-box">
                <div class="operation-box clear-float">
                    <el-button @click="utils.goUrl('/requisitionInfo?type=2')" type="primary">新增</el-button>
                    <el-button v-if="utils.getIsShopowner()" @click="exportFn" icon="el-icon-download" type="primary">导出
                    </el-button>
                </div>
                <el-table v-loading="loading" :data="tableData" header-row-class-name="has-gutter-table"
                    style="width: 100%" border ref="multipleTable" @selection-change="handleSelectionChange"
                    @sort-change="sortChange">
                    <el-table-column type="selection" width="55" align="center"> </el-table-column>
                    <el-table-column prop="id" width="50" label="序号" align="center"></el-table-column>
                    <el-table-column prop="number" label="领用单号" align="center"></el-table-column>
                    <el-table-column prop="tech_num" label="商品类型" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.goods_type | goods_typeFilter }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="goods_name" label="商品系列" align="center"></el-table-column>
                    <el-table-column prop="count" label="领用数量" align="center"></el-table-column>
                    <el-table-column prop="create_user" label="申请人" align="center"></el-table-column>
                    <el-table-column prop="tech_num" label="时间" align="center">
                        <template slot-scope="scope">
                            {{ $moment(scope.row.create_time * 1000).format('YYYY-MM-DD') }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="tech_num" label="订单状态" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.status | statusFilter }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template slot-scope="scope">
                            <span @click="setRow(1, scope.row)" class="tab-table-text">详情</span>
                            <span @click="setRow(2, scope.row)" class="tab-table-text"
                                v-if="!scope.row.status">取消</span>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="vs-paging-box">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page.sync="paging.page" :page-size="paging.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="paging.total"
                        :page-sizes="[100, 200, 500, 1000]"> </el-pagination>
                </div>
            </div>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loading: false,
            paging: {
                goods_name: '',
                status: '',
                start_time: '',
                end_time: '',
                date: '',
                page: 1,
                total: 0,
                limit: 100,
                create_user: '',
            },
            tableData: [],
            multipleSelection: '',
        };
    },
    filters: {
        goods_typeFilter(val) {
            let arr = ['', '毛坯', '成品', '积分商品', '洗护用品', '其他'];
            return arr[val];
        },
        statusFilter(val) {
            let arr = ['待审核', '已审核', '已驳回', '已取消'];
            return arr[val];
        },
    },
    async created() {
        if (this.$route.path == '/requisition') {
            this.handleCurrentChange(1);
        }
    },
    methods: {
        // 导出
        async exportFn() {
            let obj = Object.assign({}, this.paging);
            obj.status = obj.status ? obj.status * 1 - 1 : -1;
            obj.start_time = obj.date ? obj.date[0] : '';
            obj.end_time = obj.date ? obj.date[1] : '';
            delete obj.date;

            let url = this.utils.data.baseURL + 'store/stock/receiveTicketListExport';
            this.utils.urlToBlob(url, '领用单', obj);

            //     let res = await this.$Http.receiveTicketListExport(obj)
            //     this.utils.downloadXlsx(res, "领用单")
        },
        //排序
        sortChange(column) {
            console.log(column);
            // this.form.sort = column.prop
            // this.form.sort_type = column.order == 'ascending' ? 'up' : 'down'
            // this.getHouseList()
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },
        async setRow(type, row) {
            if (type == 1) {
                this.utils.goUrl(`/requisitionDetail?id=${row.id}`);
            } else {
                this.$prompt('原因：', '是否确认取消订单？', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputType: 'textarea',
                }).then(async ({ value }) => {
                    var msg = {};
                    msg.id = row.id;
                    msg.reason = value;
                    let { code, data } = await this.$Http.receiveTicketBack(msg); //特殊商品
                    if (code) {
                        this.$message.success('取消成功!');
                        await this.handleCurrentChange(1);
                    }
                });
            }
        },
        async repairGetList() {
            this.loading = true;
            let obj = Object.assign({}, this.paging);
            obj.status = obj.status ? obj.status * 1 - 1 : -1;
            obj.start_time = obj.date ? obj.date[0] : '';
            obj.end_time = obj.date ? obj.date[1] : '';
            delete obj.date;
            let { code, data } = await this.$Http.receiveTicketList(obj);
            this.loading = false;
            if (code) {
                this.tableData = data.data;
                this.paging.total = data.total;
            }
        },
        reset() {
            //重置
            this.paging = this.$options.data().paging;
            this.form.date = '';
            this.handleCurrentChange(1);
        },
        handleSizeChange(val) {
            this.paging.limit = val;
            this.repairGetList();
        },
        handleCurrentChange(val) {
            this.paging.page = val;
            this.repairGetList();
        },
    },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
    width: 130px;
    height: 130px;
}
</style>
